package com.vivi.codereview.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Log {

  /**
   * Error.
   */
  public static final int E = 0;

  /**
   * Warning.
   */
  public static final int W = 1;

  /**
   * Info.
   */
  public static final int I = 2;

  /**
   * Debug.
   */
  public static final int D = 3;

  /**
   * Deep debug.
   */
  public static final int DD = 4;

  /**
   * UI separator.
   */
  private static final String UI_SEPARATOR = "**************************************************************************************************";

  /**
   * In case of exception, will be Log.logged into this file.
   */
  protected static final File LOG_FILE = new File("D:\\codereviewtooldebug.log");

  /**
   * The logger itself;
   */
  private static StringBuilder s_logger;

  /**
   * Are we in debug mode? Meaning do we want to display debug logs in system out.
   */
  private static boolean debugMode;

  /**
   * Are we in deep debug mode? Meaning do we want to display deep debug logs in system out.
   */
  private static boolean deepDebugMode;

  static {
    deleteLogFile();
    s_logger = new StringBuilder();
    s_logger.append("############################################\n");
    s_logger.append("############################################\n");
  }

  /**
   * Delete log file if already exists.
   *
   * @return
   */
  public static boolean deleteLogFile() {
    return LOG_FILE.delete();
  }

  /**
   * Save log file with content of logger.
   *
   * @return
   */
  public static boolean saveLogFile() {
    return writeFile(getLog(), LOG_FILE);
  }

  /**
   * Add entry in the log, to be displayed only in case of exception
   *
   * @param str
   */
  public static void log(String str, int mode) {
    switch (mode) {
    case DD:
      if (!deepDebugMode) {
        break;
      }
    case D:
      if (!debugMode && !deepDebugMode) {
        break;
      }
    case I:
    case W:
    case E:
      System.out.println(str);
      s_logger.append(str);
      s_logger.append("\n");
      break;
    default:
      System.out.println("Invalid log type " + mode);
    }
  }

  /**
   * Returns content of logger.
   *
   * @return String
   */
  public static String getLog() {
    return s_logger.toString();
  }

  /**
   * Add a separator entry in log.
   */
  public static void logSeparator() {
    log(UI_SEPARATOR, I);
  }

  /**
   * Switch on debug mode.
   */
  public static void activateDebugMode() {
    debugMode = true;
  }

  /**
   * Switch off debug mode.
   */
  public static void stopDebugMode() {
    debugMode = false;
  }

  /**
   * Get location of log file.
   *
   * @return
   */
  public static String getLogFileLocation() {
    return LOG_FILE.getAbsolutePath();
  }

  /**
   * Switch on deep debug mode.
   */
  public static void activateDeepDebugMode() {
    deepDebugMode = true;
  }

  /**
   * Switch off deep debug mode.
   */
  public static void stopDeepDebugMode() {
    deepDebugMode = false;
  }

  /**
   * Write some content in a given file.
   *
   * @param content
   *          content to be written
   * @param file
   *          the file you want to write into
   * @return true if was done successfully, false otherwise
   */
  public static boolean writeFile(String content, File file) {
    boolean success = false;
    if (file.exists()) {
      file.delete();
    }
    try {
      file.createNewFile();
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(content.getBytes());
      fos.close();
      success = true;
    }
    catch (IOException e) {
      System.out.println("Cannot write log file in " + getLogFileLocation());
      e.printStackTrace();
    }
    return success;
  }

  /**
   * All the Exit codes.
   *
   * @author nvillard
   *
   */
  public final class Exit {

    public static final int CONTINUE = -1;

    public static final int OK = 0;

    public static final int GENERIC_ERROR = 1;

    public static final int REQUIRED_USER_ENTRY = 2;

    public static final int MVN_PROJECT_NOT_FOUND = 3;

    public static final int IO_EXCEPTION = 4;

    public static final int XML_PARSING_EXCEPTION = 5;

    public static final int XML_SAX_EXCEPTION = 6;

    public static final int UNKOWN_CC_STATUS = 7;

    public static final int ZIP_FILE_NOT_FOUND = 8;

    public static final int FILE_NOT_EXIST = 9;

    public static final int FILE_INVALID_STATE = 10;

    public static final int EXTERN_PROCESS_INTERRUPTED = 11;

    public static final int TOO_MANY_ATTEMPTS_FOR_USER_ENTRY = 12;

    public static final int ZIP_NOT_CREATED = 13;

    public static final int ZIP_CREATION_ERROR = 14;

    public static final int NO_JUNIT_EXIT = 15;

    public static final int NO_JDEVCC_FOLDER = 16;
  }
}
